package com.wry.wry_test.AQS;

import java.util.Random;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

/**
 * @Author: wangrongyi
 * @Date: 2024/11/11 15:06
 * @Description:
 */
public class PhaserTest {

    private final static Random RANDOM = new Random();
    public static void main(String[] args) {
        final Phaser phaser = new Phaser(3);

        for (int i = 1; i < 4; i++) {
            new Athletes(i,phaser).start();
        }
    }

    static class Athletes extends Thread {
        private final int no;
        private final Phaser phaser;


        Athletes(int no, Phaser phaser) {
            this.no = no;
            this.phaser = phaser;

        }

        @Override
        public void run() {
            try {
                System.out.println(no + " start running.");
                TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(100));
                System.out.println(no + " end running.");
                phaser.arriveAndAwaitAdvance();

                System.out.println(no + " start bicycle.");
                TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(100));
                System.out.println(no + " end bicycle.");
                phaser.arriveAndAwaitAdvance();


                System.out.println(no + " start long jump.");
                TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(100));
                System.out.println(no + " end long jump.");
                phaser.arriveAndAwaitAdvance();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
